using System;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using Deepcode.Flux.Core.Services.FluxClient;

namespace Deepcode.Flux.Core.UI.Controls
{
	/// <summary>
	/// PanelBox control provides a simple box surface on which to render other
	/// controls and components. It allows the specification of a title, and optional
	/// width and height to fix the size of the box.
	/// </summary>
	[ParseChildren(false)]
	public class PanelBox : Deepcode.Flux.Core.UI.Controls.Base.BaseControl
	{
		#region Properties
		/// <summary>
		/// Specify the title text to appear in the top of the panel
		/// </summary>
		public string Title
		{
			get
			{
				string t = (string) ViewState["title"];
				return t==null ? String.Empty : t;
			}
			set
			{
				ViewState["title"] = value;
			}
		}
		#endregion
		#region Private fields
		// Will store the absolute resolved path to the component images
		private string _ClientImages = "";
		/// <summary>
		/// Get the resolved path to the component images
		/// </summary>
		private string ClientImages
		{
			get
			{
				if( _ClientImages == "" )
						_ClientImages = ClientHelper.GetFluxClientFile("PanelBox/Img/");
				return _ClientImages;
			}
		}
		#endregion
		#region Start / End Rendering
		/// <summary>
		/// Render the opening of the panel
		/// </summary>
		/// <param name="writer"></param>
		public override void RenderBeginTag(System.Web.UI.HtmlTextWriter writer)
		{
			RenderOpenTable(writer);
			RenderTopRow(writer);
			RenderCenterRowStart(writer);
		}
		/// <summary>
		/// Render the closing of the panel
		/// </summary>
		/// <param name="writer"></param>
		public override void RenderEndTag(System.Web.UI.HtmlTextWriter writer)
		{
			RenderCenterRowEnd(writer);
			RenderBottomRow(writer);
			RenderCloseTable(writer);
		}
		#endregion
		#region Virtual methods for rendering
		/// <summary>
		/// Renders the opening of the main content row. 
		/// </summary>
		/// <param name="writer"></param>
		protected virtual void RenderCenterRowStart( HtmlTextWriter writer )
		{
			writer.Write("<tr><td colspan='3' height='100%'>");
			writer.Write("<table width='100%' height='100%' cellspacing='0' cellpadding='0' border='0'>");
			writer.Write("<tr>");
			writer.Write("  <td width='2' background='{0}{1}'><img src='{0}{1}' width='2' height='3' alt='' border='0'/></td>",
				ClientImages, "paneLeftTile.jpg");
			writer.Write(" <td width='100%' height='100%' valign='top' class='PaneContent'>");
		}
		/// <summary>
		/// Renders the closing of the main content row
		/// </summary>
		/// <param name="writer"></param>
		protected virtual void RenderCenterRowEnd( HtmlTextWriter writer )
		{
			writer.Write(" </td>");
			writer.Write("<td width='7' background='{0}{1}'><img src='{0}{1}' width='7' height='3' alt='' border='0'/></td>",
				ClientImages, "paneRightTile.jpg");
			writer.Write("</tr>");
			writer.Write("</table>");
			writer.Write("</td></tr>");
		}
		/// <summary>
		/// Renders the beginning of the panel's encapsulating table
		/// </summary>
		/// <param name="writer"></param>
		protected virtual void RenderOpenTable( HtmlTextWriter writer )
		{
			string sWidth = "";
			string sHeight = "";
			if( this.Width != Unit.Empty ) sWidth = " width='" + this.Width.ToString() + "'";
			if( this.Height != Unit.Empty ) sHeight = " height='" + this.Height.ToString() + "'";
			
			writer.Write( "<table ID='{2}' NAME='{2}' cellspacing=0 cellpadding=0 border=0{0}{1}>", sWidth, sHeight, this.ClientID );
		}
		/// <summary>
		/// Renders the closing of the panel's encapsulating table
		/// </summary>
		/// <param name="writer"></param>
		protected virtual void RenderCloseTable( HtmlTextWriter writer )
		{
			writer.Write("</table>");
		}
		/// <summary>
		/// Renders the top title row for the panel
		/// </summary>
		/// <param name="writer"></param>
		protected virtual void RenderTopRow( HtmlTextWriter writer )
		{
			if( this.Title != String.Empty )
			{
				writer.Write("<tr>");
				writer.Write(" <td width='13' height='25'><img src='{0}{1}' width='13' height='25' alt='' border='0'/></td>",
					ClientImages, "paneTopLeft.jpg");
				writer.Write(" <td width='100%' ID='{3}' height='25' class='PaneTitleBar' align='left' background='{0}{1}'>{2}</td>",
					ClientImages, "paneTopTile.jpg", Title, (this.ClientID + "Title"));
				writer.Write(" <td width='12' height='25'><img src='{0}{1}' width='12' height='25' alt='' border='0'/></td>",
					ClientImages, "paneTopRight.jpg");
				writer.Write("</tr>");
			}
			else
			{
				writer.Write("<tr>");
				writer.Write(" <td width='13' height='11'><img src='{0}{1}' width='13' height='11' alt='' border='0'/></td>",
					ClientImages, "paneTopLeftNoTitle.jpg");
				writer.Write(" <td width='100%' height='11' class='PaneTitleBar' background='{0}{1}'><img src='{0}{1}' width='10' height='11' alt='' border='0'/></td>",
					ClientImages, "paneTopTileNoTitle.jpg");
				writer.Write(" <td width='12' height='11'><img src='{0}{1}' width='12' height='11' alt='' border='0'/></td>",
					ClientImages, "paneTopRightNoTitle.jpg");
				writer.Write("</tr>");
			}
		}
		/// <summary>
		/// Renders the bottom closing row for the panel
		/// </summary>
		/// <param name="writer"></param>
		protected virtual void RenderBottomRow( HtmlTextWriter writer )
		{
			writer.Write("<tr>");
			writer.Write(" <td width='13' height='7'><img src='{0}{1}' width='13' height='7' alt='' border='0'/></td>",
				ClientImages, "paneBottomLeft.jpg");
			writer.Write(" <td width='100%' height='7' background='{0}{1}'><img src='{0}{1}' width='2' height='7' alt='' border='0'/></td>",
				ClientImages, "paneBottomTile.jpg");
			writer.Write(" <td width='12' height='7'><img src='{0}{1}' width='12' height='7' alt='' border='0'/></td>",
				ClientImages, "paneBottomRight.jpg");
			writer.Write("</tr>");
		}
		#endregion
		#region Initialisation
		/// <summary>
		/// Init will ensure that the common UI stylesheets etc are included
		/// </summary>
		/// <param name="e"></param>
		protected override void OnInit(EventArgs e)
		{
			base.OnInit (e);
			this.LoadClientStyleSheet("PanelBox/PanelBox.css", "Deepcode.Flux.Core.UI.Controls.PanelBox");		
		}
		#endregion
	}
}
